import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import Scan from "../views/homes/inchildren/Scan";
import Search from "../views/homes/inchildren/Search";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/start",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      // {
      //   //
      //   path: "/",
      //   redirect: "/home/index",
      // },
      {
        path: "index",
        name: "index",
        component: () =>
          import(/* webpackChunkName: "index" */ "../views/homes/Index"),
      },
      {
        path: "find",
        name: "find",
        component: () =>
          import(/* webpackChunkName: "find" */ "../views/homes/Find"),
      },
      {
        path: "release",
        name: "release",
        component: () =>
          import(/* webpackChunkName: "release" */ "../views/homes/Release"),
      },
      {
        path: "customized",
        name: "customized",
        component: () =>
          import(
            /* webpackChunkName: "customized" */ "../views/homes/Customized"
          ),
      },
      {
        path: "my",
        name: "my",
        component: () =>
          import(/* webpackChunkName: "my" */ "../views/homes/My"),
      },
      {
        path: "seckill",
        name: "seckill",
        component: () =>
          import(
            /* webpackChunkName: "seckill" */ "../views/homes/inchildren/Seckill"
          ),
      },
    ],
  },
  {
    path: "/scan",
    component: Scan,
  },
  {
    path: "/search",
    name: "search",
    component: Search,
  },
  // 登录
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/homes/login/denglu"),
  },
  // 注册
  {
    path: "/reg",
    name: "reg",
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/homes/login/Reg"),
  },
  // 启动页
  {
    path: "/start",
    name: "start",
    component: () =>
      import(/* webpackChunkName: "rqlist" */ "../views/homes/login/zhuangtai"),
  },
  // 人气榜单
  {
    path: "/rqlist",
    name: "rqlist",
    component: () =>
      import(
        /* webpackChunkName: "rqlist" */ "../views/homes/inchildren/Rqlist"
      ),
  },
  // 福利社
  {
    path: "/welfare",
    name: "welfare",
    component: () =>
      import(
        /* webpackChunkName: "welfare" */ "../views/homes/inchildren/Welfare"
      ),
  },
  // 查批号
  {
    path: "/batch",
    name: "batch",
    component: () =>
      import(
        /* webpackChunkName: "batch" */ "../views/homes/inchildren/BatchNumber"
      ),
  },
  // 查批号品牌跳转
  {
    path: "/brands",
    name: "brands",
    component: () =>
      import(
        /* webpackChunkName: "seckill" */ "../views/homes/inchildren/Brands"
      ),
  },
  // 分类
  {
    path: "/add",
    name: "add",
    component: () =>
      import(/* webpackChunkName: "seckill" */ "../views/homes/inchildren/Add"),
  },
  // 分类跳回
  {
    path: "/classify",
    name: "classify",
    component: () =>
      import(
        /* webpackChunkName: "seckill" */ "../views/homes/inchildren/Classify"
      ),
  },

  // 抢购跳转购物车
  {
    path: "/snapped",
    name: "snapped",
    component: () =>
      import(
        /* webpackChunkName: "seckill" */ "../views/homes/inchildren/Snapped"
      ),
  },
  // 肤质测试带面部
  {
    path: "/skins",
    name: "skins",
    component: () =>
      import(
        /* webpackChunkName: "seckill" */ "../views/homes/inchildren/Skin"
      ),
  },
  //妆品对比
  {
    path: "/cosmetics",
    name: "cosmetics",
    component: () =>
      import(
        /* webpackChunkName:"cosmetics" */ "../views/homes/inchildren/Cosmetics"
      ),
  },
  // 肤质测试
  {
    path: "/skin",
    name: "skin",
    component: () =>
      import(/* webpackChunkName: "skin" */ "../views/homes/inchildren/fuzhi"),
  },
  // 有接口的肤质测试
  {
    path: "/skinTest",
    name: "SkinTest",
    component: () =>
      import(
        /* webpackChunkName: "skin" */ "../views/homes/inchildren/SkinTest.vue"
      ),
  },
  // 首页点评
  {
    path: "/comment",
    name: "comment",
    component: () =>
      import(
        /* webpackChunkName: "comment" */ "../views/homes/inchildren/Comment"
      ),
  },
  // 协议
  {
    path: "/agreement",
    name: "agreement",
    component: () =>
      import(
        /* webpackChunkName: "Personal" */ "../views/homes/login/agreement"
      ),
  },
  {
    path: "/personal",
    // component: Personal,
    component: () =>
      import(
        /* webpackChunkName: "Personal" */ "../views/homes/mychildren/Personal.vue"
      ),
  },
  {
    path: "/brand",
    name: "Brand",
    component: () =>
      import(
        /* webpackChunkName: "Brand" */ "../views/homes/mychildren/Brand.vue"
      ),
  },

  {
    path: "/products",
    name: "Products",
    // component: Products,
    component: () =>
      import(
        /* webpackChunkName: "Products" */ "../views/homes/mychildren/Products.vue"
      ),
  },
  {
    path: "/invite",
    name: "Invite",
    // component: Invite,
    component: () =>
      import(
        /* webpackChunkName: "Invite" */ "../views/homes/mychildren/Invite.vue"
      ),
  },
  {
    path: "/myComments",
    // component: Mycomments,
    component: () =>
      import(
        /* webpackChunkName: "Mycomments" */ "../views/homes/mychildren/Mycomments.vue"
      ),
  },
  {
    path: "/pointsCenter",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "PointsCenter" */ "../views/homes/mychildren/PointsCenter.vue"
      ),
  },
  /* 积分规则跳转 */
  {
    path: "/pointsrules",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "PointsCenter" */ "../views/homes/mychildren/Pointsrules.vue"
      ),
  },
  /* 积分明细跳转 */
  {
    path: "/pointsdetails",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "PointsCenter" */ "../views/homes/mychildren/Pointsdetails.vue"
      ),
  },
  /* 我的消息跳转 */
  {
    path: "/mymessage",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Mymessage" */ "../views/homes/mychildren/Mymessage.vue"
      ),
  },
  /* 我的设置跳转 */
  {
    path: "/mysettings",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Mysettings" */ "../views/homes/mychildren/Mysettings.vue"
      ),
  },
  /* 我的订单跳转 */
  {
    path: "/myorders",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Myorders" */ "../views/homes/mychildren/Myorders.vue"
      ),
  },
  /* 我的试用跳转 */
  {
    path: "/myreport",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Myreport" */ "../views/homes/mychildren/Myreport.vue"
      ),
  },
  /* 我的设置跳转 */
  {
    path: "/myuse",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Myuse" */ "../views/homes/mychildren/Myuse.vue"
      ),
  },
  /* 我的优惠券跳转 */
  {
    path: "/mycoupons",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Mycoupons" */ "../views/homes/mychildren/Mycoupons.vue"
      ),
  },
  /* 我的资料跳转 */
  {
    path: "/myprofile",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Myprofile" */ "../views/homes/mychildren/Myprofile.vue"
      ),
  },
  /* 我的账号安全跳转 */
  {
    path: "/mysafety",
    // component: PointsCenter,
    component: () =>
      import(
        /* webpackChunkName: "Mycoupons" */ "../views/homes/mychildren/Mysafety.vue"
      ),
  },
];

// 解决异常promise抛出问题
const lanj = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) return lanj.call(this, location, resolve, reject);
  return lanj.call(this, location).catch((e) => {});
};

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
});
// 全局进入页面前拦截
router.beforeEach((to, from, next) => {
  // to  ==>  去往的页面
  // from ==>  从那个页面来
  if (to.path === "/home/my" && !localStorage.getItem("token")) {
    next("/login");
  } else {
    // 无需拦截，进入下一个页面
    next();
  }

  // next执行下一步  (必须调用next才能访问到下一个页面)
});
export default router;
